#coding=utf-8

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from decorator.auth import front_login_required
from web.models.user.repair.device import Device
from web.models.admin.user.student import Student
import time
# from decorator.auth import admin_login_required, admin_auth
from lib import libupload
from lib.app import libtype


@route(r'/user/device/?', name='user.device')
class IndexHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)

        # device, total = yield Device().select_device(school_id, 0, self.offset)
        user = self.get_scookie()
        drom = yield Student().select_dorm(school_id)
        print drom
        if len(drom) >= 1:
            room = yield Student().select_room(school_id, drom[0]["Fid"])
        mtype = libtype.get_type_dict()

        # if (float(total) / self.offset * 10 % 10) > 0:
        #     total = total / self.offset + 1
        self.render("user/device/index.html", user=user, drom=drom, room=room, mtype=mtype)

@route(r'/user/device/getroom?', name='user.getroom')
class GetroomHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)
        dorm = self.get_argument('dorm', default='')

        ret = yield Device().find_room(school_id, dorm)

        self.render("user/device/getroom.html", data=ret, name='room', dorm=dorm)

@route(r'/user/device/getdevice?', name='user.getdevice')
class GetdeviceHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)
        dorm = self.get_argument('dorm', default='')
        room = self.get_argument('room', default='')

        ret = yield Device().find_device(school_id, dorm, room)

        self.render("user/device/getdevice.html", data=ret, name="device")

@route(r'/user/device/add?', name='user.device_add')
class AddHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        self.render("user/device/add.html",)

    @asynchronous
    @gen.coroutine
    @front_login_required
    def post(self):
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fimg = self.request.files.get('Fimg')
        Fremark = self.get_argument('Fremark', default='')
        Did = self.get_argument('id', default='')
        Froom_id = self.get_scookie()['user']['Froom']
        Fdorm_id = self.get_scookie()['user']['Fdorm_id']

        if Did == '':
            self.redirect("/user/device")
            return

        if Fimg is not None:
            Fimg, path = libupload.save(Fimg[0])
            libupload.img.thumbnail(path, path)
        else:
            Fimg = ""
        print Did
        info = yield Device().get_info(school_id, Did)
        data = {
            "Fuser_id":self.get_scookie()['user']['Fid'],
            "Fuser_name":self.get_scookie()['user']['Fname'],
            "Fuser_phone":self.get_argument('tel', default=self.get_scookie()['user']['Ftel']),

            "Fdevice_type_id":info['Ftype_id'],
            "Fdevice_type_name":info['Ftype_name'],
            "Fdevice_id":info['Fid'],
            "Fdevice_name":info['Fname'],

            "Fdorm_id":info['Fdorm_id'],
            "Fdorm_name":info['Fdorm_name'],
            "Froom_id":info['Froom_id'],
            "Faddress":info['Froom_name'],
            "Fstatus":1,
            "Fschool_id":school_id,
            "Fcreate_time":time.time(),
            "Fremark":Fremark,
            "Fimg":Fimg,
        }

        count = yield Device().save_device_log(data)

        self.redirect("/user/device/cat")



@route(r'/user/device/cat?', name='user.device_cat')
class CatHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)
        s = self.get_argument('s', default='0')

        Fstudent_id = self.get_scookie()['user']['Fid']
        device, total = yield Device().select_old_device(school_id, Fstudent_id, 0, self.offset, s)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1
        self.render("user/device/cat.html", data=device, total=total, s=s)

@route(r'/user/device/clist?', name='user.device_clist')
class cListHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)
        s = self.get_argument('s', default='0')

        Fstudent_id = self.get_scookie()['user']['Fid']
        device, total = yield Device().select_old_device(school_id, Fstudent_id, int(cur_page) - 1, self.offset, s)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1

        self.render("user/device/clist.html", data=device, s=s)

@route(r'/user/device/list?', name='user.device_list')
class ListHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        Fstudent_id = self.get_scookie()['user']['Fid']
        device, total = yield Device().select_device(school_id, int(cur_page) - 1, self.offset)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1

        self.render("user/device/list.html", data=device)

@route(r'/user/device/status?', name='user.device_status')
class StatusHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        Fid = self.get_argument('id', default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)

        Fstudent_id = self.get_scookie()['user']['Fid']
        ret = yield Device().set_status(school_id, Fstudent_id, Fid)
        self.render("user/device/status.html", data=ret)
